package org.esk.dablog.gwt.dablog.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.esk.dablog.gwt.dablog.client.EntryGWTService;
import org.esk.dablog.gwt.dablog.client.model.ClientForumTopicEntry;
import org.esk.dablog.gwt.dablog.client.model.ClientForumPostEntry;
import org.esk.dablog.gwt.dablog.client.model.ClientForumComment;
import org.esk.dablog.service.EntryManager;
import org.esk.dablog.model.forum.ForumTopicEntry;
import org.esk.dablog.model.forum.ForumPostEntry;
import org.esk.dablog.model.Comment;
import org.esk.dablog.exceptions.BusinessException;
import org.esk.dablog.web.functions.EntryFunctions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.FileSystemResource;

import java.util.*;
import java.io.File;

/**
 * This class 
 * User: esk
 * Date: 04.01.2007
 * Time: 12:35:10
 * $Id:$
 */
public class EntryGWTServiceImpl extends RemoteServiceServlet implements EntryGWTService {
    private static EntryManager delegate;
    

    /**
     * lists forum's topics
     * @return
     */
    public List listForumTopics() {
        List topics = delegate.listEntries(ForumTopicEntry.class);
        List result = new ArrayList();
        for (Iterator r = topics.iterator(); r.hasNext();) {
            ForumTopicEntry entry = (ForumTopicEntry) r.next();
            ClientForumTopicEntry client = new ClientForumTopicEntry();
            client.setHeader(entry.getHeader());
            client.setId(entry.getId());
            result.add(client);
        }
        return result;
    }

    /**
     * lists posts in topic
     * @param topicId
     * @return
     */
    public List listForumPosts(long topicId) {
        try {
            ForumTopicEntry topic = (ForumTopicEntry) delegate.getEntry(ForumTopicEntry.class, topicId);
            if (topic.getPosts() == null) {
                return Collections.EMPTY_LIST;
            }
            List result = new ArrayList();
            for (Iterator r = topic.getPosts().iterator(); r.hasNext();) {
                ForumPostEntry entry = (ForumPostEntry) r.next();
                ClientForumPostEntry client = new ClientForumPostEntry();
                client.setAuthorId(entry.getAuthor().getId());
                client.setAuthorUsername(entry.getAuthor().getUsername());
                client.setDate(new Date(entry.getDate().getTime()));
                client.setHeader(entry.getHeader());
                client.setId(entry.getId());
                client.setText(entry.getText());
                result.add(client);
            }
            return result;
        }
        catch (Exception ex) {
            //TODO fix exception handling
            throw new RuntimeException(ex);
        }
    }

    /**
     * lists comments in post
     * @param id
     * @return
     */
    public List listPostComments(long id) {
        try {
            ForumPostEntry post = (ForumPostEntry) delegate.getEntry(ForumPostEntry.class, id);
            if (post.getComments() == null) {
                return Collections.EMPTY_LIST;
            }
            List result = new ArrayList();
            Map clients = new HashMap();
            for (Iterator r = post.getComments().iterator(); r.hasNext();) {
                Comment c = (Comment) r.next();
                ClientForumComment client = new ClientForumComment();
                client.setHeader(EntryFunctions.previewLength(c.getText(), 120));
                client.setAuthorId(c.getAuthor().getId());
                client.setDate(new Date(c.getDate().getTime()));
                client.setAuthorUsername(c.getAuthor().getUsername());
                client.setId(c.getId());
                client.setText(c.getText());
                clients.put(c, client);
                result.add(client);
            }
            for(Iterator r = post.getComments().iterator(); r.hasNext();) {
                Comment c = (Comment) r.next();
                ClientForumComment client = (ClientForumComment) clients.get(c);
                if (c.getParent() != null) {
                    client.setParent((ClientForumComment) clients.get(c.getParent()));
                }
            }
            return result;
        } catch (BusinessException e) {
            //TODO fix exception handling
            throw new RuntimeException(e);
        }
    }

    public void setDelegate(EntryManager delegate) {
        this.delegate = delegate;
    }

}